﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FirebirdSql.Data.FirebirdClient;

namespace file_client
{
    public partial class frmFind : Form
    {
        public frmFind()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {
            if (txtID.Text != "")
            {
                txtActionNote.Enabled = false;
                txtActionTheme.Enabled = false;
                txtAddress.Enabled = false;
                txtDetection.Enabled = false;
                txtName.Enabled = false;
                txtNick.Enabled = false;
                txtNote.Enabled = false;
                txtPhone.Enabled = false;
                txtSourceInfo.Enabled = false;
                txtSurname.Enabled = false;
                dtpFromDateAction.Enabled = false;
                dtpToDateAction.Enabled = false;
                cmbParty.Enabled = false;
                cmbRajonNonOfficial.Enabled = false;
                cmbRajonOfficial.Enabled = false;
            }
            else
            {
                txtActionNote.Enabled = true;
                txtActionTheme.Enabled = true;
                txtAddress.Enabled = true;
                txtDetection.Enabled = true;
                txtName.Enabled = true;
                txtNick.Enabled = true;
                txtNote.Enabled = true;
                txtPhone.Enabled = true;
                txtSourceInfo.Enabled = true;
                txtSurname.Enabled = true;
                dtpFromDateAction.Enabled = true;
                dtpToDateAction.Enabled = true;
                cmbParty.Enabled = true;
                cmbRajonNonOfficial.Enabled = true;
                cmbRajonOfficial.Enabled = true;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtActionNote.Text = "";
            txtActionTheme.Text = "";
            txtAddress.Text = "";
            txtDetection.Text = "";
            txtName.Text = "";
            txtNick.Text = "";
            txtNote.Text = "";
            txtPhone.Text = "";
            txtSourceInfo.Text = "";
            txtSurname.Text = "";
            dtpFromDateAction.Value = DateTime.Now;
            dtpToDateAction.Value = DateTime.Now;
            dtpFromDateAction.Checked = false;
            dtpToDateAction.Checked = false;
            cmbParty.Text = "";
            cmbRajonNonOfficial.Text = "";
            cmbRajonOfficial.Text = "";
            txtID.Text = "";
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtID.Text == "" && txtActionNote.Text == "" && txtActionTheme.Text == "" &&
                    txtAddress.Text == "" && txtDetection.Text == "" && txtName.Text == "" &&
                    txtNick.Text == "" && txtNote.Text == "" && txtPhone.Text == "" && txtSourceInfo.Text == "" &&
                    txtSurname.Text == "" && !dtpFromDateAction.Checked && !dtpToDateAction.Checked &&
                    cmbParty.Text == "" && cmbRajonNonOfficial.Text == "" & cmbRajonOfficial.Text == "")
                {
                    if (MessageBox.Show("Не выбраны критерии поиска! Поиск без указания критериев может занять продолжительное время. Желаете продолжить?", "Досье", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) return;
                }
                frmFindingList form = new frmFindingList();
                form.ListFiles = new List<int>();
                if (txtID.Text != "")
                {
                    char[] separators = {' ',';',','};
                    string[] ids = txtID.Text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string item_id in ids)
                        form.ListFiles.Add(int.Parse(item_id));
                }
                else
                {
                    FbConnection Connection = new FbConnection(Properties.Settings.Default.ConnectionString);
                    Connection.Open();
                    FbCommand cmd = Connection.CreateCommand();
                    cmd.CommandText = "FIND";
                    cmd.CommandType = CommandType.StoredProcedure;
                    FbParameter param1 = new FbParameter("SURNAME", FbDbType.VarChar, 100);
                    if (txtSurname.Text == "") param1.IsNullable = true;
                    else param1.Value = txtSurname.Text;
                    cmd.Parameters.Add(param1);
                    FbParameter param2 = new FbParameter("NAME", FbDbType.VarChar, 100);
                    if (txtName.Text == "") param2.IsNullable = true;
                    else param2.Value = txtName.Text;
                    cmd.Parameters.Add(param2);
                    FbParameter param3 = new FbParameter("NICK", FbDbType.VarChar, 100);
                    if (txtNick.Text == "") param3.IsNullable = true;
                    else param3.Value = txtNick.Text;
                    cmd.Parameters.Add(param3);
                    FbParameter param4 = new FbParameter("ADDRESS", FbDbType.VarChar, 100);
                    if (txtAddress.Text == "") param4.IsNullable = true;
                    else param4.Value = txtAddress.Text;
                    cmd.Parameters.Add(param4);
                    FbParameter param5 = new FbParameter("RO", FbDbType.Integer);
                    if (cmbRajonOfficial.SelectedIndex == -1) param5.IsNullable = true;
                    else param5.Value = cmbRajonOfficial.SelectedValue;
                    cmd.Parameters.Add(param5);
                    FbParameter param6 = new FbParameter("RNO", FbDbType.Integer);
                    if (cmbRajonNonOfficial.SelectedIndex == -1) param6.IsNullable = true;
                    else param6.Value = cmbRajonNonOfficial.SelectedValue;
                    cmd.Parameters.Add(param6);
                    FbParameter param7 = new FbParameter("PARTY", FbDbType.VarChar,1000);
                    if (cmbParty.Text == "") param7.IsNullable = true;
                    else param7.Value = cmbParty.Text;
                    cmd.Parameters.Add(param7);
                    FbParameter param8 = new FbParameter("DETECTION", FbDbType.VarChar, 1000);
                    if (txtDetection.Text == "") param8.IsNullable = true;
                    else param8.Value = txtDetection.Text;
                    cmd.Parameters.Add(param8);
                    FbParameter param9 = new FbParameter("SOURCE", FbDbType.VarChar, 1000);
                    if (txtSourceInfo.Text == "") param9.IsNullable = true;
                    else param9.Value = txtSourceInfo.Text;
                    cmd.Parameters.Add(param9);
                    FbParameter param10 = new FbParameter("NOTE", FbDbType.VarChar, 1000);
                    if (txtNote.Text == "") param10.IsNullable = true;
                    else param10.Value = txtNote.Text;
                    cmd.Parameters.Add(param10);
                    FbParameter param11 = new FbParameter("PHONE", FbDbType.VarChar, 1000);
                    if (txtPhone.Text == "") param11.IsNullable = true;
                    else param11.Value = txtPhone.Text;
                    cmd.Parameters.Add(param11);
                    FbParameter param12 = new FbParameter("FROMDATEACTION", FbDbType.TimeStamp);
                    if (!dtpFromDateAction.Checked) param12.IsNullable = true;
                    else param12.Value = dtpFromDateAction.Value;
                    cmd.Parameters.Add(param12);
                    FbParameter param13 = new FbParameter("TODATEACTION", FbDbType.TimeStamp);
                    if (!dtpToDateAction.Checked) param13.IsNullable = true;
                    else param13.Value = dtpToDateAction.Value;
                    cmd.Parameters.Add(param13);
                    FbParameter param14 = new FbParameter("THEMEACTION", FbDbType.VarChar, 1000);
                    if (txtActionTheme.Text == "") param14.IsNullable = true;
                    else param14.Value = txtActionTheme.Text;
                    cmd.Parameters.Add(param14);
                    FbParameter param15 = new FbParameter("NOTEACTION", FbDbType.VarChar, 1000);
                    if (txtActionNote.Text == "") param15.IsNullable = true;
                    else param15.Value = txtActionNote.Text;
                    cmd.Parameters.Add(param15);

                    FbDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        form.ListFiles.Add(dr.GetInt32(0));
                    }
                    dr.Close();
                    cmd.Dispose();
                    Connection.Close();     
                }
                form.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmbRajonOfficial_DropDown(object sender, EventArgs e)
        {
            FbConnection Connection = new FbConnection(Properties.Settings.Default.ConnectionString);
            Connection.Open();
            cmbRajonOfficial.Items.Clear();
            foreach (DBActive.RajonOfficial item in DBActive.RajonOfficial.LoadAll(Connection,null,cmbRajonOfficial.Text))
            {
                cmbRajonOfficial.Items.Add(item);
            }
            Connection.Close();
        }

        private void cmbRajonNonOfficial_DropDown(object sender, EventArgs e)
        {
            FbConnection Connection = new FbConnection(Properties.Settings.Default.ConnectionString);
            Connection.Open();
            cmbRajonNonOfficial.Items.Clear();
            foreach (DBActive.RajonNonOfficial item in DBActive.RajonNonOfficial.LoadAll(Connection, null, cmbRajonNonOfficial.Text))
            {
                cmbRajonNonOfficial.Items.Add(item);
            }
            Connection.Close();
        }

        private void cmbParty_DropDown(object sender, EventArgs e)
        {
            FbConnection Connection = new FbConnection(Properties.Settings.Default.ConnectionString);
            Connection.Open();
            cmbParty.Items.Clear();
            foreach (DBActive.Party item in DBActive.Party.LoadAll(Connection, null, cmbParty.Text))
            {
                cmbParty.Items.Add(item);
            }
            Connection.Close();
        }

        private void cmbRajonOfficial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbRajonOfficial.SelectedItem == null) return;
            cmbRajonOfficial.SelectedValue = (cmbRajonOfficial.SelectedItem as DBActive.RajonOfficial).id;
        }

        private void cmbRajonNonOfficial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbRajonNonOfficial.SelectedItem == null) return;
            cmbRajonNonOfficial.SelectedValue = (cmbRajonNonOfficial.SelectedItem as DBActive.RajonNonOfficial).id;
        }
    }
}
